package com.stu.boke.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.BasePageHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.boke.entity.Article;
import com.stu.boke.entity.Comment;
import com.stu.boke.enums.ArticleStatus;
import com.stu.boke.mapper.ArticleMapper;
import com.stu.boke.mapper.CommentMapper;
import com.stu.boke.service.CommentService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {
	
	@Autowired(required = false)
	private CommentMapper commentMapper;

	@Autowired(required = false)
	private ArticleMapper articleMapper;
	
	@Override
	public Comment insertSubmitComment(Comment comment) {
		try {
			commentMapper.insert(comment);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("创建评论失败, comment:{}, cause:{}", comment, e);
		}
		return comment;
	}


	@Override
	public void updateComment(Comment comment) {
		try {
			commentMapper.update(comment);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("更新评论失败, comment:{}, cause:{}", comment, e);
		}
	}

	@Override
	public void deleteById(Integer id) {
		try {
			commentMapper.deleteById(id);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("删除评论失败, comment:{}, cause:{}",  e);
		}
	}


	@Override
	public Comment getCommentById(Integer id) {
		return commentMapper.getCommentById(id);
	}


	@Override
	public List<Comment> listComment() {
		List<Comment> commentList = null;
        try {
            commentList = commentMapper.listComment();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获得评论列表失败：cause:{}", e);
        }
        return commentList;
	}


	@Override
	public List<Comment> listRecentComment(int limit) {
		 List<Comment> commentList = null;
	        try {
	            commentList = commentMapper.listRecentComment(limit);
	            for (int i = 0; i < commentList.size(); i++) {
	                Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId());
	                commentList.get(i).setArticle(article);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	            log.error("获得最新评论失败, limit:{}, cause:{}", limit, e);
	        }
	        return commentList;
	}


	@Override
	public PageInfo<Comment> listCommentByPage(Integer pageIndex, Integer pageSize) {
		BasePageHelper.startPage(pageIndex, pageSize);
        List<Comment> commentList = null;
        try {
            commentList = commentMapper.listComment();
            for (int i = 0; i < commentList.size(); i++) {
                Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId());
                commentList.get(i).setArticle(article);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("分页获得评论失败,pageIndex:{}, pageSize:{}, cause:{}", pageIndex, pageSize, e);
        }
        return new PageInfo<>(commentList);
	}


	@Override
	public List<Comment> listCommentByArticleId(Integer articleId) {
		return commentMapper.listCommentByArticleId(articleId);
	}


}
